package com.nifostasky.dlx.producer;

import com.nifostasky.dlx.config.RabbitTtlConfig;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 具有过期时间的消息 生产者
 */
@Component
public class TtlProducer {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 消息无过期时间
     */
    public void sendMsg() {
        rabbitTemplate.convertAndSend(RabbitTtlConfig.EXCHANGE_NAME, "ttl.user", "这是一条有生命周期的消息。");
    }

    /**
     * 消息带有过期时间 且过期时间小于队列过期时间 10s
     */
    public void sendMsg2() {
        MessageProperties mp = new MessageProperties();
        mp.setExpiration("10000");
        Message message = new Message("这是一条有生命周期的消息。".getBytes(), mp);
        rabbitTemplate.convertAndSend(RabbitTtlConfig.EXCHANGE_NAME, "ttl.user2", message);
    }
}
